﻿using System;
using System.Collections.Generic;

namespace RxjhServer
{
	public class MapClass
	{
		public Dictionary<int, NpcClass> npcTemplate = new Dictionary<int, NpcClass>();

		public Dictionary<int, NpcClass> MosterTemp = new Dictionary<int, NpcClass>();

		private int _MapID;

		public int MapID
		{
			get
			{
				return this._MapID;
			}
			set
			{
				this._MapID = value;
			}
		}

		public static int GetNpcConn()
		{
			int num = 0;
			foreach (MapClass current in World.Map.Values)
			{
				num += current.npcTemplate.Count;
			}
			return num;
		}

		public static Dictionary<int, NpcClass> GetMosterTemplate(int mapp)
		{
			MapClass mapClass;
			if (World.Map.TryGetValue(mapp, out mapClass))
			{
				return mapClass.MosterTemp;
			}
			return new Dictionary<int, NpcClass>();
		}

		public static Dictionary<int, NpcClass> GetnpcTemplate(int mapp)
		{
			MapClass mapClass;
			if (World.Map.TryGetValue(mapp, out mapClass))
			{
				return mapClass.npcTemplate;
			}
			return new Dictionary<int, NpcClass>();
		}

		public static NpcClass GetNpc(int mapp, int NpcWordId)
		{
			MapClass mapClass;
			if (!World.Map.TryGetValue(mapp, out mapClass))
			{
				return null;
			}
			NpcClass result;
			if (mapClass.npcTemplate.TryGetValue(NpcWordId, out result))
			{
				return result;
			}
			return null;
		}

		public static void delnpc(int mapp, int NpcWordId)
		{
			MapClass mapClass;
			if (World.Map.TryGetValue(mapp, out mapClass))
			{
				mapClass.del(NpcWordId);
			}
		}

		public void del(int NpcWordId)
		{
			using (new Lock(this.npcTemplate, "MapClass-del"))
			{
				this.npcTemplate.Remove(NpcWordId);
			}
		}

		public void add(NpcClass npc)
		{
			int i = 10000;
			while (i < 30000)
			{
				if (!this.npcTemplate.ContainsKey(i))
				{
					npc.FLD_INDEX = i;
					if (this.npcTemplate.ContainsKey(npc.FLD_INDEX))
					{
						break;
					}
					this.npcTemplate.Add(npc.FLD_INDEX, npc);
					if (npc.IsNpc == 1)
					{
						this.MosterTemp.Add(npc.FLD_INDEX, npc);
						return;
					}
					break;
				}
				else
				{
					i++;
				}
			}
		}
	}
}
